package com.everflourish.yeah100.utils;

import com.everflourish.yeah100.entity.marking.Topic;
import com.everflourish.yeah100.entity.marking.TopicModel;
import com.everflourish.yeah100.utils.collections.TopicCollections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 题型工具
 *
 * @author pf
 * @date 2015-4-20
 */
public class TopicUtil {

    private static int mMaxTopicNo = 200;

    /**
     * 获取开始题号的集合
     *
     * @param currentTopicStartNo
     * @param currentTopicEnd
     * @param currentPosition
     * @param topicModelList
     * @return
     */
    public static List<String> getStartNos(int currentTopicStartNo,
                                           int currentTopicEnd, int currentPosition,
                                           List<TopicModel> topicModelList) {
        List<String> topicNos = new ArrayList<String>();
        // 如果只有一个题型，则该题型是当前编辑的题型
        if ((topicModelList.size() == 1 && currentTopicStartNo != -1) || topicModelList.size() == 0) {
            for (int i = 0; i < mMaxTopicNo; i++) {
                topicNos.add((i + 1) + "");
            }
            return topicNos;
        }
        Topic lastTopic = new Topic();
        lastTopic.setTopicBegin(0);
        lastTopic.setTopicEnd(0);
        for (int i = 0; i < topicModelList.size(); i++) {
            if (i == currentPosition) {
                continue;
            }
            Topic topic = topicModelList.get(i).getTopic();
            int startNo = topic.getTopicBegin();
            int endNo = lastTopic.getTopicEnd();
            int count = startNo - endNo;
            for (int j = 1; j < count; j++) {
                topicNos.add((endNo + j) + "");
            }
            lastTopic = topic;
        }
        int tempMax = 0;
        if (topicModelList.size() > 1 && topicModelList.size() - 1 == currentPosition) {// 如果当前的位置是最后一个
            // 获取前一个题型的结束题号+1
            tempMax = topicModelList.get(topicModelList.size() - 2).getTopic().getTopicEnd() + 1;
        } else {// 如果当前位置不是最后一个，则获取最后题型的结束题号+1
            tempMax = topicModelList.get(topicModelList.size() - 1).getTopic().getTopicEnd();
        }
        for (int i = tempMax; i <= mMaxTopicNo; i++) {
            topicNos.add(i + "");
        }
        return topicNos;
    }

    /**
     * 获取结束题号的集合
     *
     * @param currentTopicStartNo
     * @param currentPosition
     * @param topicModels
     * @return
     */
    public static List<String> getEndNos(int currentTopicStartNo,
                                         int currentPosition, List<TopicModel> topicModels) {
        List<String> topicNos = new ArrayList<String>();
        // 如果只有一个题型，则该题型是当前编辑的题型
        if ((topicModels.size() == 1 && currentTopicStartNo != -1) || topicModels.size() == 0) {
            for (int i = currentTopicStartNo; i < mMaxTopicNo; i++) {
                topicNos.add(i + "");
            }
            return topicNos;
        }
        Topic lastTopic = new Topic();
        lastTopic.setTopicBegin(0);
        lastTopic.setTopicEnd(0);
        for (int i = 0; i < topicModels.size(); i++) {
            if (i == currentPosition) {
                continue;
            }
            Topic topic = topicModels.get(i).getTopic();
            int startNo = topic.getTopicBegin();
            int endNo = lastTopic.getTopicEnd();
            if (currentTopicStartNo > endNo && currentTopicStartNo < startNo) {
                for (int j = currentTopicStartNo; j < startNo; j++) {
                    topicNos.add(j + "");
                }
                break;
            }
            lastTopic = topic;
        }
        if (topicNos.size() == 0) {
            for (int i = currentTopicStartNo; i <= mMaxTopicNo; i++) {
                topicNos.add(i + "");
            }
        }
        return topicNos;
    }

    /**
     * 获取当前最小的题号+1
     *
     * @param topicModels
     * @return
     */
    public static int getCurrentMinNo(List<TopicModel> topicModels) {
        int currentMinNo = 1;
        if (topicModels.size() > 0) {
            Topic topic = topicModels.get(topicModels.size() - 1).getTopic();
            currentMinNo = topic.getTopicEnd() + 1;
        }
        return currentMinNo;
    }

    /**
     * 获取题型集合中最大的题号
     *
     * @param topics
     * @return
     */
    public static int getCurrentMaxNo(List<Topic> topics) {
        if (topics == null || topics.size() == 0) {
            return 0;
        }
        Collections.sort(topics, new TopicCollections(false));
        return topics.get(topics.size() - 1).getTopicEnd();
    }

    /**
     * 获取题型非主观题总题目数
     *
     * @param topics
     * @return
     */
    public static int getTopicCount(List<Topic> topics) {
        if (topics == null || topics.size() == 0) {
            return 0;
        }
        int count = 0;
        for (Topic t : topics) {
            if (t.getTopicType() != 3) {  // 只要不是主观题
                count = t.getTopicEnd() - t.getTopicBegin() + 1 + count;
            }
        }
        return count;
    }

    /**
     * 判断设置的起止题号是否包含已有题号
     *
     * @param beginNo
     * @param endNo
     * @param topics
     * @return
     */
    public static boolean judgeIsAvailableTopic(int beginNo, int endNo, int position, List<Topic> topics) {
        boolean isAvailable = true;
        for (int i = beginNo; i <= endNo; i++) {
            for (int j = 0; j < topics.size(); j++) {
                if (j == position) {
                    continue;
                }
                Topic t = topics.get(j);
                if (i >= t.getTopicBegin() && i <= t.getTopicEnd()) {
                    return false;
                }
                continue;
            }
        }
        return isAvailable;
    }

    /**
     * 获取主观题的总分
     *
     * @param topics
     * @return
     */
    public static float getSubTopicTotalScore(List<Topic> topics) {
        if (topics == null) {
            return 0;
        }
        float totalScore = 0;
        for (Topic t : topics) {
            if (t.getTopicType() != 3) {
                continue;
            }
            float topicTotalscore = t.getTopicPoint() * (t.getTopicEnd() - t.getTopicBegin() + 1);
            totalScore += topicTotalscore;
        }
        return totalScore;
    }

    /**
     * 是否有主观题
     *
     * @param topics
     * @return
     */
    public static boolean isHaveSubTopic(List<Topic> topics) {
        if (topics == null) {
            return false;
        }
        for (Topic t : topics) {
            if (t.getTopicType() == 3) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否有客观题
     *
     * @param topics
     * @return
     */
    public static boolean isHaveObjTopic(List<Topic> topics) {
        if (topics == null) {
            return false;
        }
        for (Topic t : topics) {
            if (t.getTopicType() != 3) {
                return true;
            }
        }
        return false;
    }
}